<cfcomponent extends="mxunit.framework.TestCase"
            hint="">

  <cffunction name="setUp"  output="true">
    <cfset _i = structNew() />
    <cfset _i.component = createObject("component","org.coldquery.ColdQuery") />
  </cffunction>

  <cffunction name="testInit"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset initStruct.items= arrayNew(1) />

    <cftry>
      <cfset _i.component.init(argumentcollection=initStruct) />
      <cfcatch>
        <cfset fail(message="Init of component with empty array failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.getItems() />
    <cfset assertTrue(message="Get Items must return an array",condition=isArray(result)) />

    <cfset initStruct.items= QueryNew("a,b,c,d") />
    <cftry>
      <cfset _i.component.init(argumentcollection=initStruct) />
      <cfcatch>
        <cfset fail(message="Init of component with empty array failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.getItems() />
    <cfset assertTrue(message="Get Items must return an array from query",condition=isArray(result)) />
  </cffunction>


  <cffunction name="testGetItemsArray"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset initStruct.items= arrayNew(1) />

    <!--- create a test array --->
    <cfset tmp=structNew() />
    <cfset tmp["Numeric_key"] = 1 />
    <cfset tmp["String_key"] = "D" />
    <cfset tmp["date_key"] = "1/1/2007" />
    <cfset arrayappend(initStruct.items,tmp) />
    <cfset tmp=structNew() />
    <cfset tmp["Numeric_key"] = "2" />
    <cfset tmp["String_key"] = "E" />
    <cfset tmp["date_key"] = "4/4/2006" />
    <cfset arrayappend(initStruct.items,tmp) />
    <cfset tmp=structNew() />
    <cfset tmp["Numeric_key"] = "3" />
    <cfset tmp["String_key"] = "B" />
    <cfset tmp["date_key"] = "02/10/2008" />
    <cfset arrayappend(initStruct.items,tmp) />
    <cfset tmp=structNew() />
    <cfset tmp["Numeric_key"] = "5" />
    <cfset tmp["String_key"] = "A" />
    <cfset tmp["date_key"] = "12/30/2008" />
    <cfset arrayappend(initStruct.items,tmp) />
    <cfset tmp=structNew() />
    <cfset tmp["Numeric_key"] = "4" />
    <cfset tmp["String_key"] = "B" />
    <cfset tmp["date_key"] = "12/01/2008" />
    <cfset arrayappend(initStruct.items,tmp) />

    <cftry>
      <cfset _i.component.init(argumentcollection=initStruct) />
      <cfcatch>
        <cfset fail(message="Init of component with populated array failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.sortByColumn("numeric_key","numeric","desc").getItems() />
    <cfset assertEquals(message="Numeric_key at position 5 of array should be numeric_key=1",expected=1,actual=result[5].numeric_key) />
    <cfset assertEquals(message="String_key at position 5 of array should be String_key='D'",expected='D',actual=result[5].String_key) />
    <cfset assertTrue(message="date_key at position 5 of array should be Date_key='01/01/2007'",condition=(dateCompare("01/01/2007",result[5].Date_key) EQ 0)) />

    <cfset result = _i.component.sortByColumn("date_key","date","asc").getItems() />
    <cfset assertEquals(message="Numeric_key at position 5 of array should be numeric_key=5",expected=5,actual=result[5].numeric_key) />
    <cfset assertEquals(message="String_key at position 5 of array should be String_key='A'",expected='A',actual=result[5].String_key) />
    <cfset assertTrue(message="date_key at position 5 of array should be Date_key='12/30/2008'",condition=(dateCompare("12/30/2008",result[5].Date_key) EQ 0)) />

    <cfset result = _i.component.sortByColumn("string_key","string","asc").getItems() />
    <cfset assertEquals(message="Numeric_key at position 5 of array should be numeric_key=2",expected=2,actual=result[5].numeric_key) />
    <cfset assertEquals(message="String_key at position 5 of array should be String_key='E'",expected='E',actual=result[5].String_key) />
    <cfset assertTrue(message="date_key at position 5 of array should be Date_key='04/04/2006'",condition=(dateCompare("04/04/2006",result[5].Date_key) EQ 0)) />
  </cffunction>


  <cffunction name="testGetItemsQuery"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset initStruct.items= QueryNew("Numeric_key,Str,String_key,date_key","varchar,varchar,varchar,varchar") />

    <!--- create a test array --->
    <cfset QueryAddRow(initStruct.items,5) />
    <cfset querySetCell(initStruct.items,"Numeric_key" , 1,1) />
    <cfset querySetCell(initStruct.items,"string_key" , 'D',1) />
    <cfset querySetCell(initStruct.items,"date_key" , '1/1/2007',1) />

    <cfset querySetCell(initStruct.items,"Numeric_key" , 2,2) />
    <cfset querySetCell(initStruct.items,"string_key" , 'E',2) />
    <cfset querySetCell(initStruct.items,"date_key" , '4/4/2006',2) />

    <cfset querySetCell(initStruct.items,"Numeric_key" , 3,3) />
    <cfset querySetCell(initStruct.items,"string_key" , 'B',3) />
    <cfset querySetCell(initStruct.items,"date_key" , '2/10/2008',3) />

    <cfset querySetCell(initStruct.items,"Numeric_key" , 5,4) />
    <cfset querySetCell(initStruct.items,"string_key" , 'A',4) />
    <cfset querySetCell(initStruct.items,"date_key" , '12/30/2008',4) />

    <cfset querySetCell(initStruct.items,"Numeric_key" , '4',5) />
    <cfset querySetCell(initStruct.items,"string_key" , 'B',5) />
    <cfset querySetCell(initStruct.items,"date_key" , '12/01/2008',5) />

    <cftry>
      <cfset _i.component.init(argumentcollection=initStruct) />
      <cfcatch>
        <cfset fail(message="Init of component with populated Query failed - #cfcatch.message# = #cfcatch.detail#") />
      </cfcatch>
    </cftry>

    <cfset result = _i.component.sortByColumn("numeric_key","numeric","desc").getItems() />
    <cfset assertEquals(message="Numeric_key at position 5 of array should be numeric_key=1",expected=1,actual=result[5].numeric_key) />
    <cfset assertEquals(message="String_key at position 5 of array should be String_key='D'",expected='D',actual=result[5].String_key) />
    <cfset assertTrue(message="date_key at position 5 of array should be Date_key='01/01/2007'",condition=(dateCompare("01/01/2007",result[5].Date_key) EQ 0)) />

    <cfset result = _i.component.sortByColumn("date_key","date","asc").getItems() />
    <cfset assertEquals(message="Numeric_key at position 5 of array should be numeric_key=5",expected=5,actual=result[5].numeric_key) />
    <cfset assertEquals(message="String_key at position 5 of array should be String_key='A'",expected='A',actual=result[5].String_key) />
    <cfset assertTrue(message="date_key at position 5 of array should be Date_key='12/30/2008'",condition=(dateCompare("12/30/2008",result[5].Date_key) EQ 0)) />

    <cfset result = _i.component.sortByColumn("string_key","string","asc").getItems() />
    <cfset assertEquals(message="Numeric_key at position 5 of array should be numeric_key=2",expected=2,actual=result[5].numeric_key) />
    <cfset assertEquals(message="String_key at position 5 of array should be String_key='E'",expected='E',actual=result[5].String_key) />
    <cfset assertTrue(message="date_key at position 5 of array should be Date_key='04/04/2006'",condition=(dateCompare("04/04/2006",result[5].Date_key) EQ 0)) />
  </cffunction>


  <cffunction name="testInitWithStruct"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset initStruct.items= structNew() />
    <cfset initStruct.items.key1 = "myValue" />
    <cfset initStruct.items.key2 = structNew() />
    <cfset initStruct.items.key3 = ArrayNew(1) />
    <cfset _i.component.init(argumentcollection=initStruct) />
    <cfset result = _i.component.sortByColumn("name","string","desc").getItems() />

    <cfset assertEquals(message="name key at position 1 should by 'key3'",expected='KEY3',actual=result[1].name) />
    <cfset assertEquals(message="name key at position 3 should by 'key1'",expected='KEY1',actual=result[3].name) />

  </cffunction>

  <cffunction name="testConcat"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var i = "" />
    <cfset initStruct.items= structNew() />
    <cfset initStruct.items.key1 = "myValue" />
    <cfset initStruct.items.key2 = structNew() />
    <cfset initStruct.items.key3 = ArrayNew(1) />
    <cfset result = _i.component.new(argumentcollection=initStruct) />
    <cfset result = result.concat(result.getItems()).sortByColumn("name","string","desc") />

    <cfset assertEquals(message="Length should be 6",expected='6',actual=result.getLength()) />
    <cfset assertEquals(message="name key at position 1 should by 'key3'",expected='KEY3',actual=result.get(1).name) />
    <cfset assertEquals(message="name key at position 2 should by 'key3'",expected='KEY3',actual=result.get(2).name) />
    <cfset assertEquals(message="name key at position 5 should by 'key1'",expected='KEY1',actual=result.get(5).name) />
    <cfset assertEquals(message="name key at position 6 should by 'key1'",expected='KEY1',actual=result.get(6).name) />

  </cffunction>

  <cffunction name="testPseudoClosureFunctionality"  output="true">
    <cfset var initStruct = structNew() />
    <cfset var result = "" />
    <cfset var local = structNew() />
    <cfset var q1 = createObject("component","DummyQueryLoader").init().load() /><!--- get a query to work with --->
    <cfset local.match= _i.component.new(arrayNew(1)) /><!--- i will put some data here --->
    <cfset local.nomatch= _i.component.new(arrayNew(1)) /><!--- i will put some data here --->
    <cfset result = _i.component.new(q1) />
    <cfset result = result.concat(result.getItems()).sortByColumn("name","string","desc") />
    <cfset assertEquals(6,result.getLength(),"concated data should be 6 items")>
    <cfset debug(result.getItems())>
    <cftry>
    <cfset result = result.each("if(_it.name EQ 'lee'){_l.match = _l.match.append(_it);}else{_l.nomatch = _l.nomatch.append(_it);}return _it;",local) />
    <cfcatch type="jsonencodederror">
      <cfset debug(deserializeJson(cfcatch.detail)) />
    </cfcatch>
    </cftry>
    <cfset debug(result.getItems())>
    <cfset debug(local.match.getItems()) />
    <cfset debug(local.nomatch.getItems()) />
    <cfset assertEquals(local.match.getLength(),2 ,"match object should have 2 items")>
    <cfset assertEquals(local.nomatch.getLength(),4 ,"nomatch object should have 4 items")>
    <cfset local.allLees = true />
    <cfset local.match.each("if(_l.allLees AND _it.name NEQ 'lee'){_l.allLees = false;};return _it;",local) />
    <cfset assertTrue(local.allLees,"All Lees Should be true") />
    <cfset local.match.each("if(_l.allLees AND _it.name EQ 'lee'){_l.allLees = false;};return _it;",local) />
    <cfset assertFalse(local.allLees,"Now it should be false") />

    <cfset local.noLees = true />
    <cfset local.nomatch.each("if(_l.noLees AND _it.name EQ 'lee'){_l.noLees = false;};return _it;",local) />
    <cfset assertTrue(local.noLees,"no lees should be true") />

  </cffunction>
</cfcomponent>